﻿using UnityEngine;
using System.Collections;

public class Joystik_v2 : MonoBehaviour    
{
    private GUITexture gui;
    public Rect MarginSize = new Rect(0, 0, 60, 60);
    public float SizePCT = 20;
    private int lastFingerId;
    private Rect tmprect;


	void Start () 
    {
        gui = (GUITexture)GetComponent(typeof(GUITexture));
        MarginSize.height = Screen.height / 100.0f * SizePCT;
        MarginSize.width = MarginSize.height;      
      //  MarginSize.x = MarginSize.height - 5;
      //  MarginSize.y = MarginSize.height - 5;
        gui.pixelInset = MarginSize;
	}	
	
	void Update () 
    {
        int count = Input.touchCount;
        for (int i = 0; i < count; i++)
        {
            Touch touch = Input.GetTouch(i);
            if(MarginSize.Contains(touch.position))
            {
                lastFingerId = touch.fingerId;
            }
            if (lastFingerId == touch.fingerId)
            {                
                tmprect = gui.pixelInset;
                tmprect.x = Mathf.Clamp(touch.position.x, -1, 1);
                tmprect.y = Mathf.Clamp(touch.position.y, -1, 1);
                gui.pixelInset = tmprect;
            }
        }       
	}
}
